package com.guofen.controller.shlj;

import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljEquipmentRunningtime;
import com.guofen.service.shlj.EquipmentRunningtimeService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("/shlj/equipmentRunningtime")
@Controller
@Api(tags = "EquipmentRunningtimeController", description = "设备运行时间表")
public class EquipmentRunningtimeController {

    @Autowired
    private EquipmentRunningtimeService service;

    @ApiOperation("添加开机时间记录")
    @RequestMapping(value = "/addStartupTime",method = RequestMethod.POST)
    @ResponseBody
    public Result addStartupTime(@ApiParam(name="startupTime",value="开机时间",required=true) @RequestParam String startupTime){
        if (service.addStartupTime(startupTime))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("添加关机时间记录")
    @RequestMapping(value = "/addShutdownTime",method = RequestMethod.POST)
    @ResponseBody
    public Result addShutdownTime(@ApiParam(name="shutdownTime",value="关机时间",required=true) @RequestParam String shutdownTime,
                                  @ApiParam(name="shutdownReasion",value="关机原因",required=true) @RequestParam String shutdownReasion){
        if (service.addShutdownTime(shutdownTime,shutdownReasion))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/findForm",method = RequestMethod.GET)
    @ResponseBody
    public Result findForm(){
        if (service.findForm() != null)
            return ResultGenerator.genSuccessResult(service.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(service.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="startupTime",value="开机时间",required=true) @RequestParam String startupTime,
                         @ApiParam(name="shutdownTime",value="关机时间",required=true) @RequestParam String shutdownTime,
                         @ApiParam(name="shutdownReasion",value="关机原因",required=true) @RequestParam String shutdownReasion) {
        ShljEquipmentRunningtime record = new ShljEquipmentRunningtime();
        record.setId(id);
        record.setStartupTime(startupTime);
        record.setShutdownTime(shutdownTime);
        record.setShutdownReasion(shutdownReasion);
        return ResultGenerator.genSuccessResult(service.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(service.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(service.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
